package com.itheima.chapter11.udp.chat;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;

/*
聊天室案例
 */
public class ReceiveMessage {
    public static void main(String[] args) throws IOException {
        DatagramSocket ds= new DatagramSocket(10086);
        byte[] bys =new byte[1024];
        DatagramPacket dp =new DatagramPacket(bys,bys.length);
        while(true){
            ds.receive(dp);
            byte[] data = dp.getData();
            int length = dp.getLength();
            String s = new String(data, 0, length);
            String hostName = dp.getAddress().getHostName();
            String ip = dp.getAddress().getHostAddress();
            System.out.println("ip："+ip+"主机："+hostName+"发送了数据："+s);
        }

    }
}
